package com.quan.admin.web.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.quan.admin.entity.SysSchedulerJob;
import com.quan.admin.service.SysSchedulerJobService;
import com.quan.commons.core.bean.R;
import com.quan.commons.core.biz.support.MyBaseController;
import com.quan.commons.core.utils.PageUtils;
import io.swagger.annotations.Api;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;



/**
 * 系统-定时作业管理
 *
 * @author yhaoquan
 * @email yhaoquan@163.com
 * @date 2020-08-26 10:43:38
 */
@Api(tags = "系统-定时作业管理")
@RestController
@RequestMapping("sysschedulerjob")
public class SysSchedulerJobController extends MyBaseController {

    @Autowired
    private SysSchedulerJobService service;

    /**
     * 保存
     * @param entity
     * @return
     */
    @PostMapping("save")
    public R save(@Valid @RequestBody SysSchedulerJob entity){

        if(this.service.hasExist("job_name", entity.getJobName(), SysSchedulerJob.class)) {
            return R.failure("该值已存在");
        }
        if(null == entity.getJobGroup()) {
            entity.setJobGroup(Scheduler.DEFAULT_GROUP) ;
        }

        this.service.startJob(entity);
        return R.ok();
    }

    /**
     * 删除
     * @param params idList: [1,2,3]
     * @return
     */
    @PostMapping("delete")
    public R delete(@RequestBody Map<String, Object> params) {
        if (null != params && !params.isEmpty()) {
            List<Long> idList = JSON.parseArray(params.get("idList").toString(), Long.class);
            this.service.removeByIds(idList);
            return R.ok();
        } else {
            return R.failure();
        }
    }

    /**
     * 修改
     * @param entity
     * @return
     */
    @PostMapping("update")
    public R update(@Valid @RequestBody SysSchedulerJob entity){
		this.service.updateById(entity);
        return R.ok();
    }

    /**
     * 信息
     * @param {id}
     * @return
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        SysSchedulerJob sysSchedulerJob = this.service.getById(id);
        return R.ok().put("data", sysSchedulerJob);
    }

    /**
     * 列表
     * @param entity
     * @return
     */
    @GetMapping(value = "list")
    public Object list(SysSchedulerJob entity) {
        List<SysSchedulerJob> list = this.service.list(new PageUtils(request), entity);
        return R.ok().put("data", list);
    }

    /**
     * 分页列表
     * @param entity
     * @return
     */
    @GetMapping(value = "page")
    public Object page(SysSchedulerJob entity) {
        IPage<SysSchedulerJob> page = this.service.selectPage(new PageUtils(request), entity);
        return R.ok().put("data", page);
    }

    /**
     * 自定义分页列表
     * @param entity
     * @return
     */
    @GetMapping(value = "selectMyPage")
    public Object selectMyPage(SysSchedulerJob entity) {
        IPage<SysSchedulerJob> page = this.service.selectMyPage(new PageUtils(request), entity);
        return R.ok().put("data", page);
    }

    /**
     *  暂停任务
     * @param entity
     * @return
     */
    @GetMapping(value = "/pauseJob")
    public Object pauseJob(SysSchedulerJob entity) {
        this.service.pauseJob(entity.getId(), entity.getJobName(), entity.getJobGroup());
        return R.ok();
    }

    /**
     * 恢复任务
     * @param entity
     * @return
     */
    @GetMapping(value = "/resumeJob")
    public Object resumeJob(SysSchedulerJob entity) {
        this.service.resumeJob(entity.getId(), entity.getJobName(), entity.getJobGroup());
        return R.ok();
    }

    /**
     * 触发任务
     * @param entity
     * @return
     */
    @GetMapping(value = "/triggerJob")
    public Object triggerJob(SysSchedulerJob entity) {
        this.service.triggerJob(entity.getId(), entity.getJobName(), entity.getJobGroup());
        return R.ok();
    }


}
